package com.unionpay.upyzt.example;

import com.unionpay.upyzt.Upyzt;
import com.unionpay.upyzt.exception.UpyztException;
import com.unionpay.upyzt.model.MchApplication;
import com.unionpay.upyzt.model.Media;
import com.unionpay.upyzt.resp.MchApplicationResp;
import com.unionpay.upyzt.resp.MchApplicationStoreResp;
import com.unionpay.upyzt.resp.MediaResp;
import com.unionpay.upyzt.util.StringUtils;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import static com.unionpay.upyzt.example.Main.getResPath;

public class MchApplicationExample {

    /**
     * 运行 Demos
     *
     * @return 二级商户进件返回参数
     */
    public static MchApplicationResp runDemos() throws UpyztException {
        MchApplicationExample mchApplicationExample = new MchApplicationExample();

        // 二级商户首次进件
        MchApplicationStoreResp mchApplicationStoreResp = mchApplicationExample.create();

        // 二级商户进件状态查询（系统订单号）
        MchApplicationResp mchApplicationResp = mchApplicationExample.retrieveById(mchApplicationStoreResp.getMchApplicationId());

        // 修改二级商户信息
        if (mchApplicationResp.getMchId() != null) {
            mchApplicationResp = mchApplicationExample.renew(mchApplicationResp.getMchId());
        }

        // 二级商户进件状态查询（平台订单号）
        mchApplicationResp = mchApplicationExample.retrieveByOutRequestNo(mchApplicationResp.getOutRequestNo());

        return mchApplicationResp;
    }

    /**
     * 二级商户首次进件
     *
     * @return 返回参数
     * @throws UpyztException 异常
     */
    public MchApplicationStoreResp create() throws UpyztException {
        File file = new File(getResPath("id_card_sample.jpg"));
        MediaResp mediaResp = Media.uploadImage(file);

        Map<String, Object> businessLicence = new HashMap<>();
        businessLicence.put("copy", mediaResp.getMediaId());
        businessLicence.put("number", StringUtils.randomNumeric(18));
        businessLicence.put("company_name", "张三公司");
        businessLicence.put("company_address", "张三公司地址");
        businessLicence.put("valid_time", "2020-01-01,2050-01-01");

        Map<String, Object> legalPersonIdCard = new HashMap<>();
        legalPersonIdCard.put("type", "1");
        legalPersonIdCard.put("copy", mediaResp.getMediaId());
        legalPersonIdCard.put("national", mediaResp.getMediaId());
        legalPersonIdCard.put("number", Upyzt.encryptField("310100200001011236"));
        legalPersonIdCard.put("name", Upyzt.encryptField("法人张三"));
        legalPersonIdCard.put("valid_time", "2010-01-01,2030-01-01");

        Map<String, Object> settleAcct = new HashMap<>();
        settleAcct.put("type", "2");
        settleAcct.put("bank_code", "105100000017");
        settleAcct.put("name", Upyzt.encryptField("张三公司"));
        settleAcct.put("bank_address_code", "110105");
        settleAcct.put("bank_branch_code", "001588044013");
        settleAcct.put("bank_acct_no", Upyzt.encryptField("123456789"));

        Map<String, Object> params = new HashMap<>();
        params.put("out_request_no", Main.generateRandomOutNo());
        params.put("organization_type", "1");
        params.put("short_name", "张三公司");
        params.put("business_license", businessLicence);
        params.put("settle_acct", settleAcct);
        params.put("legal_person_id_card", legalPersonIdCard);
        params.put("legal_person_mobile_number", Upyzt.encryptField("12345678901"));
        // 通过[个人手机号验证码]接口获取的短信验证码，用于验证法人手机号; 是否必传取决于平台业务参数配置
        params.put("sms_code", "999999");
        params.put("contact_mobile_number", Upyzt.encryptField("12345678901"));
        params.put("contact_email", Upyzt.encryptField("zhangsan@example.com"));
        params.put("password", Upyzt.encryptField("pa55w0rd"));

        System.out.println("------- 二级商户首次进件 -------");
        MchApplicationStoreResp resp = MchApplication.create(params);
        System.out.println(resp);
        return resp;
    }

    /**
     * 二级商户进件状态查询（系统订单号）
     *
     * @return 返回参数
     * @throws UpyztException 异常
     */
    public MchApplicationResp retrieveById(String id) throws UpyztException {
        System.out.println("------- 二级商户进件状态查询（系统订单号） -------");
        MchApplicationResp resp = MchApplication.retrieveById(id);
        System.out.println(resp);
        return resp;
    }

    /**
     * 修改二级商户信息
     *
     * @return 返回参数
     * @throws UpyztException 异常
     */
    public MchApplicationResp renew(String mchId) throws UpyztException {
        File file = new File(getResPath("id_card_sample.jpg"));
        MediaResp mediaResp = Media.uploadImage(file);

        Map<String, Object> businessLicence = new HashMap<>();
        businessLicence.put("copy", mediaResp.getMediaId());
        businessLicence.put("company_name", "李四公司");
        businessLicence.put("company_address", "李四公司地址");
        businessLicence.put("valid_time", "2020-01-02,2050-01-02");

        Map<String, Object> legalPersonIdCard = new HashMap<>();
        legalPersonIdCard.put("type", "1");
        legalPersonIdCard.put("copy", mediaResp.getMediaId());
        legalPersonIdCard.put("national", mediaResp.getMediaId());
        legalPersonIdCard.put("name", Upyzt.encryptField("法人李四"));
        legalPersonIdCard.put("valid_time", "2010-01-02，2030-01-02");

        Map<String, Object> settleAcct = new HashMap<>();
        settleAcct.put("type", "2");
        settleAcct.put("bank_code", "105100000017");
        settleAcct.put("name", Upyzt.encryptField("法人李四"));
        settleAcct.put("bank_address_code", "110105");
        settleAcct.put("bank_branch_code", "001588044013");
        settleAcct.put("bank_acct_no", Upyzt.encryptField("123456780"));

        Map<String, Object> params = new HashMap<>();
        params.put("mch_id", mchId);
        params.put("out_request_no", Main.generateRandomOutNo());
        params.put("short_name", "张三公司");
        params.put("business_license", businessLicence);
        params.put("settle_acct", settleAcct);
        params.put("legal_person_id_card", legalPersonIdCard);
        params.put("legal_person_mobile_number", Upyzt.encryptField("12345678902"));
        // 通过[个人手机号验证码]接口获取的短信验证码，用于验证法人手机号; 是否必传取决于平台业务参数配置
        params.put("sms_code", "999999");
        params.put("contact_mobile_number", Upyzt.encryptField("12345678902"));
        params.put("contact_email", Upyzt.encryptField("zhangsan2@example.com"));

        System.out.println("------- 修改二级商户信息 -------");
        MchApplicationResp resp = MchApplication.renew(params);
        System.out.println(resp);
        return resp;
    }

    /**
     * 二级商户进件状态查询（平台订单号）
     *
     * @return 返回参数
     * @throws UpyztException 异常
     */
    public MchApplicationResp retrieveByOutRequestNo(String outRequestNo) throws UpyztException {
        System.out.println("------- 二级商户进件状态查询（系统订单号） -------");
        MchApplicationResp resp = MchApplication.retrieveByOutRequestNo(outRequestNo);
        System.out.println(resp);
        return resp;
    }

}
